﻿using System;
using System.Linq;
using ElevatorApplication.Extensions;

namespace ElevatorApplication
{
    public class UpRunningElevatorState : IElevatorState
    {
        private readonly IElevator _elevator;

        public UpRunningElevatorState(IElevator elevator)
        {
            _elevator = elevator;
        }

        public void Running()
        {
            if (!_elevator.IsHigherFloorButtonBePushed())
            {
                var state = new StoppedState(_elevator);
                _elevator.ChangeSate(state);
                Console.WriteLine("elevator is stopped in " + _elevator.CurrentFloorNumber + " floor");
                return;
            }
            var nextFloor = _elevator.GetNextHigherBePushedFloor();
            for (int i = _elevator.CurrentFloorNumber+1; i < nextFloor+1; i++)
            {
                _elevator.SetCurrentFloorNumber(i);

                Console.WriteLine("elevator is in " + i + " floor");
            }

            var buttonState =
                _elevator.ButtonStateInFloors.Single(x => x.FloorNumber == _elevator.CurrentFloorNumber);

            buttonState.ResetButtonsWhenElevatorGoingUp();

            Running();
        }


        private int GetHigherFloorNumber()
        {
            return 10;
        }

    }
}